package cn.jingyuan.owl.web.core;

import cn.jingyuan.owl.model.core.RestResult;
import cn.jingyuan.owl.model.error.ErrorCode;

public class RestResultUtils {

    private static final int SC_OK = 200;

    private static final int SC_INTERNAL_SERVER_ERROR = 500;

    public static <D> RestResult<D> success() {
        return new <D>RestResult<D>()
            .setCode(ExCode.SUCCESS.getCode())
            .setCodeMsg(ExCode.SUCCESS.getCodeMsg())
            .setStatus(SC_OK);
    }

    public static <D> RestResult<D> success(D data) {
        return new <D>RestResult<D>()
            .setCode(ExCode.SUCCESS.getCode())
            .setCodeMsg(ExCode.SUCCESS.getCodeMsg())
            .setData(data)
            .setStatus(SC_OK);
    }

    public static <D> RestResult<D> success(D data, String msg) {
        return new <D>RestResult<D>()
            .setCode(ExCode.SUCCESS.getCode())
            .setCodeMsg(msg)
            .setData(data)
            .setStatus(SC_OK);
    }

    public static <D> RestResult<D> failure() {
        return new <D>RestResult<D>()
            .setCode(ExCode.FAILURE.getCode())
            .setCodeMsg(ExCode.FAILURE.getCodeMsg())
            .setStatus(SC_INTERNAL_SERVER_ERROR);
    }

    public static <D> RestResult<D> failure(D data) {
        return new <D>RestResult<D>()
            .setCode(ExCode.FAILURE.getCode())
            .setCodeMsg(ExCode.FAILURE.getCodeMsg())
            .setData(data)
            .setStatus(SC_INTERNAL_SERVER_ERROR);
    }

    public static <D> RestResult<D> failure(D data, String msg) {
        return new <D>RestResult<D>()
            .setCode(ExCode.FAILURE.getCode())
            .setCodeMsg(msg)
            .setData(data)
            .setStatus(SC_INTERNAL_SERVER_ERROR);
    }

    public static <D> RestResult<D> warning() {
        return new <D>RestResult<D>()
            .setCode(ExCode.WARNING.getCode())
            .setCodeMsg(ExCode.WARNING.getCodeMsg())
            .setStatus(SC_INTERNAL_SERVER_ERROR);
    }

    public static <D> RestResult<D> warning(String msg) {
        return new <D>RestResult<D>()
            .setCode(ExCode.WARNING.getCode())
            .setCodeMsg(msg)
            .setStatus(SC_INTERNAL_SERVER_ERROR);
    }

    public static <D> RestResult<D> warning(D data, String msg) {
        return new <D>RestResult<D>()
            .setCode(ExCode.WARNING.getCode())
            .setCodeMsg(msg)
            .setData(data)
            .setStatus(SC_INTERNAL_SERVER_ERROR);
    }

    public static <D> RestResult<D> instance(ErrorCode exCode, boolean success) {
        RestResult<D> result = new <D>RestResult<D>()
            .setCode(exCode.getCode())
            .setCodeMsg(exCode.getCodeMsg());

        if (success) {
            result.setStatus(SC_OK);
        } else {
            result.setStatus(SC_INTERNAL_SERVER_ERROR);
        }
        return result;
    }

    public static <D> RestResult<D> instance(ErrorCode exCode, boolean success, D data, String msg) {
        RestResult<D> result = new <D>RestResult<D>()
            .setCode(exCode.getCode())
            .setCodeMsg(exCode.getCodeMsg())
            .setData(data);

        if (null != msg) {
            result.setMessage(msg);
        }

        if (success) {
            result.setStatus(SC_OK);
        } else {
            result.setStatus(SC_INTERNAL_SERVER_ERROR);
        }
        return result;
    }

}
